package br.com.Formula;

import br.com.ConexaoBanco.ConexaoMySQL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * CadastrarUsuario.java
 *
 * Created on 24/05/2011, 08:27:54
 */
/**
 *
 * @author Jayme Augusto Vara
 */
public class CadastrarParada extends javax.swing.JInternalFrame {

    /**
     * Creates new form CadastrarUsuario
     */
    public CadastrarParada() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalvar = new javax.swing.JButton();
        lblQtdComb = new javax.swing.JLabel();
        jtfQtdComb = new javax.swing.JTextField();
        lblPneu = new javax.swing.JLabel();
        jcbPneu = new javax.swing.JComboBox();
        lblPiloto = new javax.swing.JLabel();
        jcbPiloto = new javax.swing.JComboBox();
        lblVolta = new javax.swing.JLabel();
        jtfVolta = new javax.swing.JTextField();
        lblParada = new javax.swing.JLabel();
        jcbParada = new javax.swing.JComboBox();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cadastro de Piloto");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/16 x 16/add.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/filesave.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        lblQtdComb.setText("Qtd de Combustível:");

        lblPneu.setText("Pneu:");

        lblPiloto.setText("Piloto:");

        lblVolta.setText("Nr da Volta:");

        lblParada.setText("Parada:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSalvar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblQtdComb)
                            .addComponent(lblVolta))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfVolta, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(jtfQtdComb)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblParada)
                            .addComponent(lblPiloto)
                            .addComponent(lblPneu))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbPneu, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbPiloto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbParada, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbPiloto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPiloto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbParada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblParada))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbPneu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPneu))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfVolta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVolta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfQtdComb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblQtdComb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        try {
            Connection com = ConexaoMySQL.getConexaoMySQL();
            Statement stm = com.createStatement();
            String idPiloto;
            int idParada, idPneu;
            ComboItem combo = (ComboItem) jcbPiloto.getSelectedItem();
            idPiloto = combo.getValue();
            combo = (ComboItem) jcbParada.getSelectedItem();
            idParada = Integer.parseInt(combo.getValue());
            combo = (ComboItem) jcbPneu.getSelectedItem();
            idPneu = Integer.parseInt(combo.getValue());
           stm.executeUpdate("INSERT INTO cadparada VALUES ('"
                    + idPiloto + "'," + idParada + "," + idPneu + ",'" + jtfVolta.getText() + "','" + jtfQtdComb.getText() + "')");
            JOptionPane.showMessageDialog(null, "Dados salvos com sucesso!",
                    "Dados salvos com sucesso!", 1);
          //  jtfID.setText("");
            jtfQtdComb.setText("");
            jtfVolta.setText("");
            jcbPneu.setSelectedIndex(0);
            jcbPiloto.setSelectedIndex(0);
            jcbParada.setSelectedIndex(0);
        } catch (SQLException ex) {
            Logger.getLogger(CadastrarParada.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnSalvarActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        ResultSet rs;
        try {
            Connection com = ConexaoMySQL.getConexaoMySQL();
            Statement stm = com.createStatement();
            rs = stm.executeQuery("SELECT * FROM piloto");
            rs.first();
            while (!rs.isAfterLast()) {
                ComboItem combo = new ComboItem(rs.getString("id_piloto"), rs.getString("nom_piloto"));
                jcbPiloto.addItem(combo);
                rs.next();
            }
            rs = stm.executeQuery("SELECT * FROM tipoparada");
            rs.first();
            while (!rs.isAfterLast()) {
                ComboItem combo = new ComboItem(rs.getString("id_tipo"), rs.getString("nom_tipo"));
                jcbParada.addItem(combo);
                rs.next();
            }
            String pneu;
            rs = stm.executeQuery("SELECT * FROM pneu");
            rs.first();
            while (!rs.isAfterLast()) {
                pneu = rs.getString("marca_pneu") + " | " + rs.getString("tipo_pneu");
                ComboItem combo = new ComboItem(rs.getString("id_pneu"), pneu);
                jcbPneu.addItem(combo);
                rs.next();
            }
        } catch (SQLException ex) {
            Logger.getLogger(CadastrarParada.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formInternalFrameOpened
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox jcbParada;
    private javax.swing.JComboBox jcbPiloto;
    private javax.swing.JComboBox jcbPneu;
    private javax.swing.JTextField jtfQtdComb;
    private javax.swing.JTextField jtfVolta;
    private javax.swing.JLabel lblParada;
    private javax.swing.JLabel lblPiloto;
    private javax.swing.JLabel lblPneu;
    private javax.swing.JLabel lblQtdComb;
    private javax.swing.JLabel lblVolta;
    // End of variables declaration//GEN-END:variables
}
